#Load packages
library(tidyverse)
library(quantmod)
library(ggpubr)
library(readxl)
library(lubridate)
library(writexl)
library(zoo)
library(Rcpp)
library(xts)
#install.packages("ggpubr")
#install.packages("writexl")
#install.packages('Rcpp')

# Set working directory.
setwd("C:\\work_temp\\Evidence_from_real_estate_funds\\plot")
dataPath = "C:\\work_temp\\Evidence_from_real_estate_funds\\data_backup"

#TASK02 - Country Specific Plots 2020
#GERMANY
#Import data.
dataGer = read_excel(paste(dataPath, "\\Germany_2020.xlsx", sep=""))
dataGer$date = as.Date(dataGer$date, "%y/%m/%d")

#Create a legend for the plot
colors1 <- c("Deutsche Konsum REIT AG (commercial)" = "#67000d", "Deutsche Industrie REIT AG (commercial)" = "#a50f15", "Alstria Office REIT AG (commercial)" = "#cb181d", "Hamborner REIT AG (commercial)" = "#ef3b2c", "Deutsche Wohnen SE (residential)" = "#08306b", "LEG Immobilien AG (residential)" = "#08519c", "Vonovia SE (residential)" = "#2171b5", "TAG Immobilien AG (residential)" = "#4292c6", "Grand City Properties SA (residential)" = "#6baed6")

#Plot the Data
ggplot(dataGer, aes(x=date)) +
  geom_line(aes(y = Deutsche_Konsum_REIT_AG, color = "Deutsche Konsum REIT AG (commercial)")) +
  geom_line(aes(y = Deutsche_Industrie_REIT_AG, color = "Deutsche Industrie REIT AG (commercial)")) +
  geom_line(aes(y = Alstria_Office_REIT_AG, color = "Alstria Office REIT AG (commercial)")) +
  geom_line(aes(y = Hamborner_REIT_AG, color = "Hamborner REIT AG (commercial)")) +
  geom_line(aes(y = Deutsche_Wohnen_SE, color = "Deutsche Wohnen SE (residential)")) +
  geom_line(aes(y = LEG_Immobilien_AG, color = "LEG Immobilien AG (residential)")) +
  geom_line(aes(y = Vonovia_SE, color = "Vonovia SE (residential)")) +
  geom_line(aes(y = TAG_Immobilien_AG, color = "TAG Immobilien AG (residential)")) +
  geom_line(aes(y = Grand_City_Properties_SA, color = "Grand City Properties SA (residential)")) + 
  scale_x_date(breaks = c(seq(from=as.Date("2020-01-01"), to=as.Date("2021-04-30"), by="quarter")),
  labels= c("2020 Q1", "2020 Q2", "2020 Q3", "2020 Q4", "2021 Q1", "2021 Q2"), minor_breaks=c(seq(from=as.Date("2020-01-01"), to=as.Date("2021-04-01"), by="month"))) +
  ylab("Share price") +
  xlab("") +
  labs(color = "") +
  theme(plot.caption = element_text(hjust = 1)) +
  theme_minimal() +
  theme(legend.justification=c(0,1), legend.position="none", legend.text=element_text(size=8.25), legend.spacing.x = unit(0.25, 'cm'), legend.spacing.y = unit(-0.2, 'cm')) +
  guides(color = guide_legend(ncol = 2, byrow = TRUE)) +
  theme(axis.text=element_text(size=14), axis.title=element_text(size=14, face="plain"), panel.grid.major=element_line(size = 0.75), legend.background=element_rect(fill="white", colour='white')) +
  scale_color_manual(values = colors1, breaks = names(colors1)[c(7,8,5,6,9,4,1,2,3)]) 

#Save the plot as PDF
ggsave("Plot_Germany.pdf", width=11.5, height=6) 
